package cn.itcast.web.controller.cargo;

import cn.itcast.common.utils.BeanMapUtils;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.ExportProductExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/export")
public class PdfController extends BaseController {

	@Reference
	private ExportService exportService;

	@Reference
	private ExportProductService exportProductService;

	/**
	 * 通过jasper resport创建出口报运单的PDF文件
	 *  参数：报运单ID
	 */
	@RequestMapping("/exportPdf")
	public void exportPdf(String id) throws Exception {
		//1.根据id查询报运单
		Export export = exportService.findById(id);
		//2.根据报运单id查询商品
		ExportProductExample example = new ExportProductExample();
		ExportProductExample.Criteria criteria = example.createCriteria();
		criteria.andExportIdEqualTo(id);
		List<ExportProduct> list = exportProductService.findAll(example);
		//3.将报运单对象转化为map
		Map map = BeanMapUtils.beanToMap(export); //通过工具类，将export对象转化为map集合（key=属性名，value=属性值）
		//4.封装JRBeanCollectionDataSource
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
		//5.获取模板的路径
		String path = session.getServletContext().getRealPath("/")+"/jasper/export.jasper";
		//6.数据填充
		JasperPrint jasperPrint = JasperFillManager.fillReport(path, map, dataSource);
		//7.pdf文件导出
		JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
	}


	/**
	 *  chart报表
	 *
	 */
	@RequestMapping("/exportPdf5")
	public void exportPdf5() throws Exception {
		//1.获取模板的路径
		String path = session.getServletContext().getRealPath("/")+"/jasper/test6.jasper";
		//2.加载模板，创建JasperPrint对象
		List<Map> list = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			Map map = new HashMap();
			map.put("name","产品"+i);
			map.put("count",100*(i+1));
			list.add(map);
		}

		//在jasper中，将数组数据封装成了一个JRDataSourcre对象
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

		JasperPrint jasperPrint = JasperFillManager.fillReport(path, new HashMap<>(), dataSource);
		//3.输出pdf文件（预览，下载）
		JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
	}


	/**
	 *  分组报表
	 *      将list对象填充到模板中，在模板中根据配置自动的分组展示
	 */
	@RequestMapping("/exportPdf4")
	public void exportPdf4() throws Exception {
		//1.获取模板的路径
		String path = session.getServletContext().getRealPath("/")+"/jasper/test5.jasper";
		//2.加载模板，创建JasperPrint对象
		List<User> list = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			User user = new User();
			user.setUserName("用户"+i);
			user.setEmail(i+"@export.com");
			user.setCompanyName("传智播客");
			user.setDeptName("部门"+i);
			list.add(user);
		}
		for (int i = 0; i <3; i++) {
			User user = new User();
			user.setUserName("用户"+i);
			user.setEmail(i+"@export.com");
			user.setCompanyName("IT黑马");
			user.setDeptName("部门"+i);
			list.add(user);
		}

		//在jasper中，将数组数据封装成了一个JRDataSourcre对象
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

		JasperPrint jasperPrint = JasperFillManager.fillReport(path, new HashMap<>(), dataSource);
		//3.输出pdf文件（预览，下载）
		JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
	}


	/**
	 * 数据填充：list集合数据
	 */
	@RequestMapping("/exportPdf3")
	public void exportPdf3() throws Exception {
		//1.获取模板的路径
		String path = session.getServletContext().getRealPath("/")+"/jasper/test4.jasper";
		//2.加载模板，创建JasperPrint对象
		List<User> list = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			User user = new User();
			user.setUserName("用户"+i);
			user.setEmail(i+"@export.com");
			user.setCompanyName("传智播客");
			user.setDeptName("部门"+i);
			list.add(user);
		}
		//在jasper中，将数组数据封装成了一个JRDataSourcre对象
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

		JasperPrint jasperPrint = JasperFillManager.fillReport(path, new HashMap<>(), dataSource);
		//3.输出pdf文件（预览，下载）
		JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
	}


	/**
	 * 数据填充：map集合数据
	 */
	@RequestMapping("/exportPdf2")
	public void exportPdf2() throws Exception {
		//1.获取模板的路径
		String path = session.getServletContext().getRealPath("/")+"/jasper/test3.jasper";
		//2.加载模板，创建JasperPrint对象
		//在加载博班创建对象的时候，可以进行数据填充
		/**
		 * 1.模板路径
		 * 2.map类型的参数
		 * 3.list类型的参数（JrDataSource对象）
		 */
		Map map =  new HashMap<>();
		map.put("username","王者荣耀");
		map.put("age",12);
		map.put("address","中国深圳");
		map.put("company","腾讯");


		JasperPrint jasperPrint = JasperFillManager.fillReport(path, map, new JREmptyDataSource());
		//3.输出pdf文件（预览，下载）
		JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
	}




	/**
	 * 页面点击此链接，将生成的pdf预览下载
	 *  不需要返回值
	 */
	@RequestMapping("/exportPdf1")
	public void exportPdf1() throws Exception {
		//1.获取模板的路径
		String path = session.getServletContext().getRealPath("/")+"/jasper/test2.jasper";
		//2.加载模板，创建JasperPrint对象
		//在加载博班创建对象的时候，可以进行数据填充
		/**
		 * 1.模板路径
		 * 2.map类型的参数
		 * 3.list类型的参数（JrDataSource对象）
		 */
		JasperPrint jasperPrint = JasperFillManager.fillReport(path, new HashMap<>(), new JREmptyDataSource());
		//3.输出pdf文件（预览，下载）
		JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
	}
}
